package org.jweb.demo.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.jweb.core.bean.PageMode;
import org.jweb.core.dao.IHibernateCommonDao;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.jweb.core.service.impl.CommonServiceImpl;
import org.jweb.core.util.MyBeanUtils;
import org.jweb.demo.entity.DemoGoodsEntity;
import org.jweb.demo.entity.DemoGoodsTypeDicEntity;
import org.jweb.demo.entity.DemoGoodsTypeREntity;
import org.jweb.demo.service.DemoGoodsTypeServiceI;
import org.jweb.demo.trans.form.DemoGoodsTypeDicSaveForm;
import org.jweb.demo.trans.form.DemoGoodsTypeDicSearchForm;
import org.jweb.demo.trans.vo.DemoGoodsTypeDicVO;
import org.jweb.demo.trans.vo.DemoGoodsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("demoGoodsTypeService")
@Transactional
public class DemoGoodsTypeServiceImpl extends CommonServiceImpl implements DemoGoodsTypeServiceI {

	@Autowired
	private IHibernateCommonDao hibernateCommonDao;//该dao提供一些不依赖于具体前端js框架的查询

	
	@Override
	public PageMode<DemoGoodsTypeDicVO> list(
			DemoGoodsTypeDicSearchForm searchForm, int pageNo, int pageSize) {
		PageMode<DemoGoodsTypeDicVO> resultPage  = new PageMode<DemoGoodsTypeDicVO>(); 
		
		searchForm.emptyValue2NullValue();
		CriteriaQuery cq = searchForm.getCriteriaQuery();
		PageMode<DemoGoodsTypeDicEntity> goodsTypePage = this.hibernateCommonDao.list4page(cq,pageNo,pageSize);
		
		List<DemoGoodsTypeDicEntity> goodsTypeList = goodsTypePage.getResult();
		List<DemoGoodsTypeDicVO> goodsTypeVOList = new ArrayList<DemoGoodsTypeDicVO>();
		for(DemoGoodsTypeDicEntity t : goodsTypeList){
			DemoGoodsTypeDicVO goodsTypeDicVO = new DemoGoodsTypeDicVO();
			goodsTypeDicVO.copyEntity(t);
			
			//手工转换goods
			List<DemoGoodsVO> goodsList = new ArrayList<DemoGoodsVO>();
			Set<DemoGoodsTypeREntity> rSet = t.getGoodsSet();
			for(DemoGoodsTypeREntity r : rSet){
				DemoGoodsEntity goodsEntity = r.getGoodsEntity();
				DemoGoodsVO goodsVO = new DemoGoodsVO();
				
				goodsVO.copyEntity(goodsEntity);
				goodsList.add(goodsVO);
			}
			goodsTypeDicVO.setGoodsList(goodsList);
			
			
			
			//最后将goodsVO放到List集合中
			goodsTypeVOList.add(goodsTypeDicVO);
		}
		
		
		//复制实体page到vo page中
		BeanUtils.copyProperties(goodsTypePage, resultPage, new String[]{"result"});
		resultPage.setResult(goodsTypeVOList);
		return resultPage;
	}

	@Override
	public DemoGoodsTypeDicVO get(String goodsTypeId) {
		DemoGoodsTypeDicEntity entity = (DemoGoodsTypeDicEntity)this.hibernateCommonDao.get(DemoGoodsTypeDicEntity.class, goodsTypeId);
		DemoGoodsTypeDicVO vo = new DemoGoodsTypeDicVO();
		vo.copyEntity(entity);
		
		//手工封装商品信息
		vo.setGoodsList(new ArrayList<DemoGoodsVO>());
		Set<DemoGoodsTypeREntity> rSet = entity.getGoodsSet();
		for(DemoGoodsTypeREntity r : rSet){
			DemoGoodsEntity goodsEntity = r.getGoodsEntity();
			
			DemoGoodsVO goodsVO = new DemoGoodsVO();
			goodsVO.setId(goodsEntity.getId());
			goodsVO.setName(goodsEntity.getName());
			
//			dicVO.copyEntity(dicEntity);
			vo.getGoodsList().add(goodsVO);
		}
		
		
		return vo;
	}

	@Override
	public void add(DemoGoodsTypeDicSaveForm saveForm) {
		DemoGoodsTypeDicEntity entity = (DemoGoodsTypeDicEntity)saveForm.toEntity();
		this.hibernateCommonDao.save(entity);
		
	}

	@Override
	public void update(DemoGoodsTypeDicSaveForm saveForm) {
		DemoGoodsTypeDicEntity newEntity = (DemoGoodsTypeDicEntity)saveForm.toEntity();
		DemoGoodsTypeDicEntity oldEntity = (DemoGoodsTypeDicEntity)this.hibernateCommonDao.get(DemoGoodsTypeDicEntity.class, saveForm.getId());
		
		try {
			MyBeanUtils.copyBeanNotNull2Bean(newEntity, oldEntity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.hibernateCommonDao.update(oldEntity);
		
	}

	@Override
	public void add(DemoGoodsTypeDicEntity entity) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(DemoGoodsTypeDicEntity entity) {
		// TODO Auto-generated method stub
		
	}

}
